package com.gzx.plugin.data.xxljob;

import com.gzx.plugin.data.core.consts.CompanyConstants;
import com.gzx.plugin.data.core.sync.record.entity.DataSyncRecord;
import com.gzx.plugin.data.core.sync.record.service.DataSyncRecordService;
import com.gzx.plugin.data.core.utils.BatchNumberUtils;
import com.gzx.plugin.data.wxjk.entity.DataWxjkSssj;
import com.gzx.plugin.data.wxjk.service.IDataWxjkSssjService;
import com.gzx.plugin.data.wxjk.service.ITjzyWxjkService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;

/**
 * @author zjt
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WxjkDataSynchronizationJob {

    private final DataSyncRecordService dataSyncRecordService;

    private final IDataWxjkSssjService dataWxjkSssjService;

    private final ITjzyWxjkService tjzyWxjkService;

    @XxlJob("wxDataSynchronizationJob")
    public void wxDataSynchronizationJob() {
        try {
            TenantContext.disableTenantFilter();
            if (dataWxjkSssjService.isStartSynchronization() && dataSyncRecordService.getRecordByCurrentMonthAndCompanyCode(CommonDateUtil.getCurrentMonth(), CompanyConstants.WXJK_COMPANY_CODE)) {
                log.info("维信案件数据同步......开始");
                long startTime = System.currentTimeMillis();
                //获取当前月份所有数据ID
                List<Long> currentMonthIdList = dataWxjkSssjService.getCurrentMonthIdList();
                //通过ID获取所有数据
                List<DataWxjkSssj> dataWxjkSssjList = dataWxjkSssjService.getAllDataByIdList(currentMonthIdList);
                if (!CollectionUtils.isEmpty(dataWxjkSssjList)) {
                    tjzyWxjkCaseProcess(dataWxjkSssjList);
                    DataSyncRecord record = new DataSyncRecord();
                    record.setCompanyCode(CompanyConstants.WXJK_COMPANY_CODE);
                    record.setCurrentMonth(CommonDateUtil.getCurrentMonth());
                    record.setCreateTime(CommonDateUtil.getCurrentTimeStr());
                    dataSyncRecordService.insert(record);
                    log.info("维信案件数据同步......结束，耗时:{}", System.currentTimeMillis() - startTime);
                }
            }
        } catch (Exception e) {
            log.error("微财案件数据同步出现异常", e);
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }

    private void tjzyWxjkCaseProcess(List<DataWxjkSssj> dataList) {
        if (!CollectionUtils.isEmpty(dataList)) {
            String batchNumber = BatchNumberUtils.getBatchNumber(CompanyConstants.WXJK_COMPANY_CODE);
            tjzyWxjkService.batchCase(dataList, batchNumber);
            tjzyWxjkService.batchCaseDetail(dataList, batchNumber);
            tjzyWxjkService.batchDebtorInfo(dataList);
            tjzyWxjkService.batchDebtorRelationPhone(dataList);
        }
    }
}
